import json
import urllib.request

# send_data = {
#     'secret': '37d77ea083f0b60fb590938816882a53',
#     'app_key': '1e235ecc4daa3e046997580ef48f7238',
#     'template_id': 'RV-jBDQSNen0J92hKn5GGE4id6We5Xu2vDgpbXbWe2I',
#     'url': '',
#     'data': {
#         'first': {
#             'value': '{{first}}',
#             'color': '#173177'
#         },
#         'keyword1': {
#             'value': '{{keyword1}}',
#             'color': '#173177'
#         },
#         'keyword2': {
#             'value': '{{keyword2}}',
#             'color': '#173177'
#         },
#         'keyword3': {
#             'value': '{{keyword3}}',
#             'color': '#173177'
#         },
#         'keyword4': {
#             'value': '{{keyword4}}',
#             'color': '#173177'
#         },
#         'keyword5': {
#             'value': '{{keyword5}}',
#             'color': '#173177'
#         },
#         'remark': {
#             'value': '{{remark}}',
#             'color': '#173177'
#         },
#     }
# }

send_data = {'secret': '37d77ea083f0b60fb590938816882a53', 'app_key': '1e235ecc4daa3e046997580ef48f7238',
             'template_id': 'RV-jBDQSNen0J92hKn5GGE4id6We5Xu2vDgpbXbWe2I',
             'url': ''}

req_data = {'first': {
    'value': '{{first}}',
    'color': '#173177'
}, 'keyword1': {
    'value': '{{keyword1}}',
    'color': '#173177'
}, 'keyword2': {
    'value': '{{keyword2}}',
    'color': '#173177'
}, 'keyword3': {
    'value': '{{keyword3}}',
    'color': '#173177'
}, 'keyword4': {
    'value': '{{keyword4}}',
    'color': '#173177'
}, 'keyword5': {
    'value': '{{keyword5}}',
    'color': '#173177'
}, 'remark': {
    'value': '{{remark}}',
    'color': '#173177'
}}


def send_req(first, keyword1, keyword2, keyword3, keyword4, keyword5, remark):
    url = 'http://jy.erpit.cn/api/message/send'
    headers = {'content-type': 'application/json'}
    req_data['first']['value'] = first
    req_data['keyword1']['value'] = keyword1
    req_data['keyword2']['value'] = keyword2
    req_data['keyword3']['value'] = keyword3
    req_data['keyword4']['value'] = keyword4
    req_data['keyword5']['value'] = keyword5
    req_data['remark']['value'] = remark

    send_data['data'] = req_data
    params = json.dumps(send_data, ensure_ascii=False)
    # 将str类型转换为bytes类型
    params_data = bytes(params, 'utf8')
    req = urllib.request.Request(url=url, data=params_data, headers=headers, method='POST')
    response = urllib.request.urlopen(req)
    html = response.read()
    result = json.loads(html)
    return result


req_data_2 = {'first': {
    'value': '{{first}}',
    'color': '#173177'
}, 'keyword1': {
    'value': '{{keyword1}}',
    'color': '#173177'
}, 'keyword2': {
    'value': '{{keyword2}}',
    'color': '#173177'
}, 'remark': {
    'value': '{{remark}}',
    'color': '#173177'
}}


def send_qywx_rep(first, keyword1, keyword2, remark):
    """发送企业微信工作周报提醒"""
    url = 'http://jy.erpit.cn/api/message/send'
    headers = {'content-type': 'application/json'}
    req_data_2['first']['value'] = first
    req_data_2['keyword1']['value'] = keyword1
    req_data_2['keyword2']['value'] = keyword2
    req_data_2['remark']['value'] = remark

    send_data['template_id'] = "VT0cby39emhLEZGMR-rxf0GBnNBP_Ia4ZYlqLmwjEYs"
    send_data['data'] = req_data_2

    params = json.dumps(send_data, ensure_ascii=False)
    # 将str类型转换为bytes类型
    params_data = bytes(params, 'utf8')
    req = urllib.request.Request(url=url, data=params_data, headers=headers, method='POST')
    response = urllib.request.urlopen(req)
    html = response.read()
    result = json.loads(html)
    return result
